package fangbing.controller;

import java.io.IOException;
import java.io.Writer;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import fangbing.model.MenstruationBase;
import fangbing.model.vo.Events;
import fangbing.model.vo.MenstruationBaseVo;
import fangbing.service.MenstruationAlgorithmService;
import fangbing.service.MenstruationService;
import fangbing.util.common.GsonUtils;

@Controller
public class MenstruationController {
	
	@Autowired
	private MenstruationService menstruationService;
	@Autowired
	private MenstruationAlgorithmService menstruationAlgorithmService;
	
	/**
	 * 显示日历
	 * */
	@RequestMapping("/menstruation/search")
	public ModelAndView searchCalendar(MenstruationBase menstruationBase) {
		ModelAndView modelAndView = new ModelAndView("menstruation/menstruationBase");
		return modelAndView;
	}
	
	
	/**
	 * 添加月经计算基本信息
	 * */
	@RequestMapping("/menstruation/add")
	public ModelAndView addMenstruationBase(MenstruationBaseVo menstruationBaseVo) {
		ModelAndView modelAndView = new ModelAndView("redirect:/menstruation/getMenstruationBaseByUserId.do");
		int userId = 84;//userId为84是fangbing暂时写死。
		menstruationBaseVo.setUserId(userId);
		menstruationService.addMenstruationBase(menstruationBaseVo);
		return modelAndView;
	}
	
	/**
	 * 修改月经计算基本信息
	 * */
	@RequestMapping("/menstruation/update")
	public ModelAndView updateMenstruationBase(MenstruationBaseVo menstruationBaseVo) {
		ModelAndView modelAndView = new ModelAndView("redirect:/menstruation/getMenstruationBaseByUserId.do");
		try {
			int userId = 84;//userId为84是fangbing暂时写死。
			menstruationBaseVo.setUserId(userId);
			menstruationService.updateMenstruationBase(menstruationBaseVo);
		} catch (Exception e) {
			
		}
		
		return modelAndView;
	}
	
	/**
	 * 查询当前用户的月经基本信息
	 * */
	@RequestMapping("/menstruation/getMenstruationBaseByUserId")
	public ModelAndView getMenstruationBaseByUserId(MenstruationBaseVo menstruationBaseVo){
		
		ModelAndView modelAndView = new ModelAndView("menstruation/menstruationBaseAdd");
		Map<String, Object> map = new HashMap<String, Object>();
		Integer userId = 84;//userId为84是fangbing暂时写死。
		menstruationBaseVo = menstruationService.getMenstruationBaseByUserId(userId);
		map.put("menstruationBaseVo", menstruationBaseVo);
		modelAndView.addAllObjects(map);
		return modelAndView;
	}
	
	/**
	 * 在日历中显示用户上个月本月和下个月的月经信息
	 * @throws ParseException 
	 * */
	@RequestMapping("/menstruation/getMenstruationBaseInfo")
	public void getMenstruationBaseInfo(HttpServletResponse response,HttpServletRequest request) throws ParseException{
		//当前月日历开始日期
		String start = request.getParameter("start");
		
		List<HashMap<String,String>> list  = new ArrayList<HashMap<String,String>>();
		
		//获取用户录入的月经数据
		MenstruationBaseVo menstruationBaseVo = menstruationService.getMenstruationBaseByUserId(84);
		
		
		List<Events> eventsList = new ArrayList<Events>();
		
		//计算当前月历每天的类型
		eventsList = menstruationAlgorithmService.getDateType(start,menstruationBaseVo);
		
		//将所有日期放入map中
		for(Events events : eventsList){
			HashMap<String,String> map = new HashMap<String,String>();
			map.put("start", events.getStart());
			map.put("end", events.getEnd());
			map.put("title", events.getTitle());
			map.put("color", events.getColor());
			list.add(map);
		}
		
		Writer writer;
		try {
			writer = response.getWriter();
			writer.write(GsonUtils.toJson(list));
			writer.flush();
			writer.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	


	public MenstruationService getMenstruationService() {
		return menstruationService;
	}


	public void setMenstruationService(MenstruationService menstruationService) {
		this.menstruationService = menstruationService;
	}


	public MenstruationAlgorithmService getMenstruationAlgorithmService() {
		return menstruationAlgorithmService;
	}


	public void setMenstruationAlgorithmService(
			MenstruationAlgorithmService menstruationAlgorithmService) {
		this.menstruationAlgorithmService = menstruationAlgorithmService;
	}
	
	
	

}
